#= require jquery2
#= require jquery.turbolinks
#= require jquery_ujs
#= require bootstrap.min
#= require bootstrap-select.min
#= require underscore
#= require backbone
#= require jquery.autogrow-textarea
#= require topics
#= require turbolinks
#= require google_analytics
#= require_self
AppView= Backbone.View.extend
  el: "body"
  windowInActive: true

  events:
    "click a.button-follow-user": "followUser"
    "change input[type=checkbox]": "selectall"

  initialize: ->

#  selectall: (e) ->
#    toggleall=$(e.target)
##    checkId = $(e.target).attr("id")
#    a=$(" .checkbox")
##    $('input:checkbox').eq(1).attr("checked",'true')
#
#    if (toggleall.attr("id") == 'checkall')
##      alert("a")
#      if toggleall.prop("checked") == true
##        alert("true")
#        $("input:checkbox").attr("checked","true")
##        $('input:checkbox').each  ->
##          $('input:checkbox').eq(1).attr("checked",'true')
#      else
##        $('input:checkbox').each ->
##          $(this).attr('checked',false)
#
##    return false

  followUser: (e) ->
    btn = $(e.currentTarget)
    userId = btn.data("id")
    span = btn.find("span")
    follow_count= $(".follow_infos .following .count")
    if btn.hasClass("active")
      $.ajax
        url: "/users/#{userId}/unfollow"
        type: "POST"
        success: (res) ->
          if res.code == 0
            btn.removeClass('active')
            span.text("关注")
            follow_count.text(res.data.followers_count)
    else
      $.ajax
        url: "/users/#{userId}/follow"
        type: 'POST'
        success: (res) ->
          if res.code == 0
            btn.addClass('active').attr("title", "")
            span.text("取消关注")
            follow_count.text(res.data.followers_count)
    return false

# which key is login, and value is the name.
$(document).on 'page:change',  ->
  window._appView = new AppView()